package org.pangu.service.impl;

import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.*;
import com.alibaba.dubbo.rpc.service.GenericService;

//@org.springframework.stereotype.Service
public class DubboGenericService implements SmartInitializingSingleton {

	@Override
	public void afterSingletonsInstantiated() {
		ServiceConfig<GenericService> service = new ServiceConfig<GenericService>();
	    ApplicationConfig application = new ApplicationConfig("test-provider");
	    service.setApplication(application);
	    RegistryConfig registryConfig = new RegistryConfig();
	    registryConfig.setAddress("zookeeper://114.67.85.120:2181");
	    service.setRegistry(registryConfig);
	    service.setInterface("com.suibian.WhateverService");
	 
	    GenericService genericService = (method, parameterTypes, args) -> {
	        if ("method1".equals(method)) {
	            return "method1 result:" + args[0];
	        }
	        if ("method2".equals(method)) {
	            return 12345;
	        }
	        if (parameterTypes.length == 2
	                && parameterTypes[0].equals("java.lang.String")
	                && parameterTypes[1].equals("java.lang.Integer")
	                && "method3".equals(method)) {
	            return "method3,param1:" + args[0] + ",param2:" + args[1];
	        }
	        return null;
	    };
	    service.setRef(genericService);
	    service.export();
	}
}
